package com.allwees.bs.task.order.auto;


import com.allwees.bs.module.oms.entity.AccountTransit;
import com.allwees.bs.module.oms.entity.AccountTransitMaxId;
import com.allwees.bs.module.oms.service.AccountService;
import com.allwees.bs.module.oms.service.AccountTransitMaxIdService;
import com.allwees.bs.module.oms.service.AccountTransitService;
import com.allwees.core.common.util.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service

public class AccountTransitTask {

    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountTransitService accountTransitService;

    @Autowired
    private AccountTransitMaxIdService accountTransitMaxIdService;


    @XxlJob(value="accountTransitTask")
    public ReturnT<String> run(String s) {
        doTask();
        return ReturnT.SUCCESS;
    }

    private void doTask() {
        log.info("============accountTransitTask start====================");
        AccountTransitMaxId accountTransitMaxId = accountTransitMaxIdService.getById(1L);
        if (null == accountTransitMaxId) {
            //log.info("============accountTransitTask end, accountTransitMaxId is null ====================");
            return;
        }
        Integer id = accountTransitMaxId.getAccountTransitMaxId();
        AccountTransit   accountTransit = accountTransitService.getById(++id);
        int max = 50;
        int i = 0;

        while (null == accountTransit) {
            accountTransit = accountTransitService.getById( ++id);
            if (null != accountTransit && accountTransit.getIsDo().intValue() == 0) {
                break;
            }
            ++ i;
            if ( i > max) {
                break;
            }
        }

        while (null != accountTransit) {
            if (accountTransit.getIsDo().intValue() == 1) {
                accountTransitMaxId.setAccountTransitMaxId(accountTransit.getId() );
                accountTransitMaxIdService.updateById(accountTransitMaxId);
                accountTransit = accountTransitService.getById( ++id);
                continue;
            }
            boolean op = false;
            switch (accountTransit.getAccountTransitAction()) {
                case "doWaitingShipAmt":
                    op = accountService.doWaitingShipAmt(accountTransit);
                    break;
                case "doInShippingAmt":
                    op = accountService.doInShippingAmt(accountTransit);
                    break;
                case "doUnSettle":
                    op = accountService.doUnSettle(accountTransit);
                    break;
                case "doSettle":
                    op = accountService.doSettle(accountTransit);
                    break;
                case "doRefundAmtShipped":
                    op = accountService.doRefundAmtShipped(accountTransit);
                    break;
                case "doRefundAmtWaitShip":
                    op = accountService.doRefundAmtWaitShip(accountTransit);
                    break;
                case "doPlatformRefundAmt":
                    op = accountService.doPlatformRefundAmt(accountTransit);
                    break;
                case "doRetailerRefundAmt":
                    op = accountService.doRetailerRefundAmt(accountTransit);
                    break;
                default :
                   log.info("wrong account transit action, order_no:{}, order_uuid:{}, order_item_uuid: {}",accountTransit.getOrderNo(),accountTransit.getOrderUuid(),accountTransit.getOrderItemUuid());
            }
            if (op) {
               accountTransit.setIsDo(1);
               accountTransitService.setIsDoById(accountTransit.getId(), DateUtil.now());
               accountTransitMaxId.setAccountTransitMaxId(accountTransit.getId() );
               accountTransitMaxIdService.updateById(accountTransitMaxId);
               accountTransit = accountTransitService.getById(++id);
            }
        }
       //log.info("============accountTransitTask end====================,accountTransitMaxId:{}",--id);




    }


}
